package com.lenovo.hawk.controller;

import com.lenovo.hawk.config.DruidConfig;
import com.lenovo.hawk.service.CompService;
import com.lenovo.hawk.service.CustomerService;
import com.lenovo.hawk.service.MybatisTestService;
import org.apache.ibatis.session.defaults.DefaultSqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class mytestController {
    @Value("${spring.profiles.active}")
    private String ACTIVE_PROFILE;
    @Autowired
    private ApplicationContext applicationContext;
    @Autowired
    private DruidConfig druidConfig;
    @Autowired
    CustomerService customerService;
    @Autowired
    CompService compService;
    @Autowired
    MybatisTestService mybatisTestService;

    @RequestMapping(value = "/myTest")
    public String myTest() throws Exception {
        DefaultSqlSessionFactory defaultSqlSessionFactory = null;
        Object sqlSessionFactoryBean = applicationContext.getBean("sqlSessionFactoryMaster");
//        SqlSessionFactoryBean sqlSessionFactoryBean = (SqlSessionFactoryBean) applicationContext.getBean("sqlSessionFactoryMaster");
//        sqlSessionFactoryBean.setDataSource(druidConfig.druidOracleDataSource());
//        druidConfig.getSqlSessionFactoryBeanMap().get("sqlSessionFactorySlave").setDataSource(druidConfig.druidOracleDataSource());
//        druidConfig.getSqlSessionFactoryBeanMap().get("sqlSessionFactorySlave").afterPropertiesSet();
//        druidConfig.getDynamicDataSourceMap().get("sqlSessionFactorySlave").setDefaultTargetDataSource(druidConfig.druidOracleDataSource());
//        druidConfig.getDynamicDataSourceMap().get("sqlSessionFactorySlave").afterPropertiesSet();
        druidConfig.getDynamicDataSourceMap().get("sqlSessionFactorySlave").setTargetDataSource(druidConfig.druidOracleDataSource());
        return ACTIVE_PROFILE;
    }

    @RequestMapping(value = "/test01")
    public String mybatisTest01() {
        System.out.println(ACTIVE_PROFILE);
        Integer dataResult = compService.findAll();
        System.out.println(dataResult);
        return "测试构建项目请求成功" + dataResult;
    }

    @RequestMapping(value = "/test02")
    public String mybatisTest02() {
        System.out.println(ACTIVE_PROFILE);
        Integer data = customerService.findAll();
        System.out.println(data);
        return "测试构建项目请求成功" + data;
    }

    @RequestMapping(value = "/test03")
    public String mybatisTest03() {
        System.out.println(ACTIVE_PROFILE);
        Integer data = mybatisTestService.findAllByMaster();
        System.out.println(data);
        Integer data2 = mybatisTestService.findAllBySlave();
        System.out.println(data2);
        return "测试构建项目请求成功 master:" + data + ",slave:" + data2;
    }

}
